package com.poobo.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.poobo.aop.SystemCtrLo4jAop;
import com.poobo.condiction.ResourcesConfigCondiction;
import com.poobo.entity.TbConfigList;
import com.poobo.entity.TbResourcesConfig;
import com.poobo.service.IConfigListService;
import com.poobo.service.IEquipmentConfigListService;
import com.poobo.service.IResourcesConfigService;
import com.poobo.service.IResourcesService;
import com.poobo.util.pager.Page;
import com.poobo.util.property.PropertyUtil;
import com.poobo.util.str.StrUtil;

@Controller
@RequestMapping("configList")
public class ConfigListCtr {
	@Autowired
	private IConfigListService configListService;
	@Autowired
	private IResourcesConfigService resourcesConfigService;
	@Autowired
	private IResourcesService resourcesService;
	@Autowired
	private IEquipmentConfigListService equipmentConfigListService;
	/**
	 * 跳转到配置列表页面
	 * @param request
	 * @param configList
	 * @param page
	 * @param condiction
	 * @param page2
	 * @return
	 */
	@RequestMapping("toConfigListPage")
	public String toConfigListPage( HttpServletRequest request,TbConfigList configList,Page page, 
			ResourcesConfigCondiction condiction,Page page2 ){
		if( configList.getName() != null ){
			request.setAttribute("name",configList.getName());
		}if( configList.getIsEnable() != null ){
			if( configList.getIsEnable() == 1 ){
				request.setAttribute("isEnable","是");
			}else if( configList.getIsEnable() == 0 ){
				request.setAttribute("isEnable","否");
			}else{
				request.setAttribute("isEnable","");
			}
		}if( configList.getIsGenerationTxt() != null ){
			if( configList.getIsGenerationTxt() == 0 ){
				request.setAttribute("isGenerationTxt","未生成");
			}else if( configList.getIsGenerationTxt() == 1 ){
				request.setAttribute("isGenerationTxt","已生成");
			}else{
				request.setAttribute("isGenerationTxt","");
			}
		}
		page = configListService.queryConfigListPage(configList, page);
		request.setAttribute("pageNo", page.getPageNo());
		request.setAttribute("pageNum", page.getPageNum());
		request.setAttribute("pageSize", page.getPageSize());
		request.setAttribute("rowCount", page.getRowCount());
		request.setAttribute("configLists", page.getPageList());
		page2 = resourcesConfigService.queryResourcesConfigPage(condiction, page2);
		request.setAttribute("resourcesConfigs", page2.getPageList());
		return "configList/config_list";
	}
	/**
	 * 跳转到新增页面
	 * @param request
	 * @param configList
	 * @return
	 */
	@RequestMapping("toSaveConfigList")
	public String toSaveConfigList(HttpServletRequest request,TbConfigList configList){
		return "configList/save_configList";
	}
	/**
	 * 跳转到编辑页面
	 * @param request
	 * @param configList
	 * @return
	 */
	@RequestMapping("toEditConfigList")
	public String toEditConfigList(HttpServletRequest request,TbConfigList configList){
		configList = configListService.queryConfigListById(Integer.parseInt(request.getParameter("configListId")));
		request.setAttribute("configList", configList);
		return "configList/edit_configList";
	}
	/**
	 * 新增配置列表
	 * @param request
	 * @param configList
	 * @return
	 */
	@RequestMapping("saveConfigList")
	@SystemCtrLo4jAop(description="新增配置列表")
	public String saveConfigList(HttpServletRequest request,TbConfigList configList){
		configListService.saveConfigList(configList);
		return "redirect:/configList/toConfigListPage.html";
	}
	/**
	 * 编辑配置列表
	 * @param request
	 * @param configList
	 * @return
	 */
	@RequestMapping("editConfigList")
	@SystemCtrLo4jAop(description="编辑配置列表")
	public String editConfigList(HttpServletRequest request,TbConfigList configList){
		configListService.saveConfigList(configList);
		return "redirect:/configList/toConfigListPage.html";
	}
	/**
	 * 根据主键删除
	 * @param request
	 * @return
	 */
	@RequestMapping("deleteById")
	@SystemCtrLo4jAop(description="删除单个配置列表")
	public String deleteById(HttpServletRequest request){
		Integer id = Integer.parseInt(request.getParameter("configListId"));
		configListService.deleteConfigListById(id);
		resourcesConfigService.deleteResourcesConfigByConfigListId(id);
		equipmentConfigListService.deleteEquipmentConfigByConfigListId(id);
		String path = PropertyUtil.configProperty.getProperty("resource_disk_base_path") + File.separator + PropertyUtil.configProperty.getProperty("resource_ConfigListTxt");
		File file = new File( path + File.separator + id + ".txt" );
		if (file.exists()) {
			file.delete();
		}
		return "redirect:/configList/toConfigListPage.html";
	}
	@RequestMapping("deleteByIds")
	@SystemCtrLo4jAop(description="批量删除配置列表")
	public String deleteByIds(HttpServletRequest request){
		String ids = request.getParameter("ids");
		String[] idss = ids.split(",");
		Integer id = null;
		for (String idStr : idss) {
			id = Integer.parseInt(idStr);
			configListService.deleteConfigListById(id);
			resourcesConfigService.deleteResourcesConfigByConfigListId(id);
			equipmentConfigListService.deleteEquipmentConfigByConfigListId(id);
			String path = PropertyUtil.configProperty.getProperty("resource_disk_base_path") + File.separator + PropertyUtil.configProperty.getProperty("resource_ConfigListTxt");
			File file = new File( path + File.separator + id + ".txt" );
			if (file.exists()) {
				file.delete();
			}
		}
		return "redirect:/configList/toConfigListPage.html";
	}
	/**
	 * 生成配置列表详情的txt
	 * @param request
	 * @param configList
	 * @return
	 */
	@RequestMapping("generateTxt")
	@SystemCtrLo4jAop(description="生成配置列表的txt文件")
	public String generateTxt( HttpServletRequest request, TbConfigList configList ){
		Integer configListId = Integer.parseInt(request.getParameter("configListId"));
		//Integer configListId = configList.getId();
		configList = configListService.queryConfigListById(configListId);
		List<TbResourcesConfig> resourcesConfigs = resourcesConfigService.queryResourcesConfigByConfigListId(configListId);
		try {
			String path = PropertyUtil.configProperty.getProperty("resource_disk_base_path") + File.separator + PropertyUtil.configProperty.getProperty("resource_ConfigListTxt");
			File dir = new File(path);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			File file = new File( path + File.separator + configListId + ".txt" );
			if (!file.exists()) {
				file.createNewFile();
			}else{
				file.delete();
			}
			PrintWriter pw = new PrintWriter(file);
			//第一行为列表的资源数量
			pw.print(resourcesConfigs.size());
			pw.print("&");
			pw.println();
			for( int i=0; i<resourcesConfigs.size(); i++ ){
				Short type = 0;
				String resourcesURL = null;
				String version = null;
				String resourcesName = null;
				Integer reid = resourcesConfigs.get(i).getResourcesId();//资源ID
				if( reid != null ){					
					type = resourcesService.queryResourcesById(resourcesConfigs.get(i).getResourcesId()).getType();//资源类型
					resourcesURL = resourcesService.queryResourcesById(resourcesConfigs.get(i).getResourcesId()).getPath();
					version = resourcesService.queryResourcesById(resourcesConfigs.get(i).getResourcesId()).getVersion();
					if( type==0 ){					
						resourcesName = resourcesService.queryResourcesById(resourcesConfigs.get(i).getResourcesId()).getName();
					}else{
						resourcesName = null;
					}
					if( StrUtil.isNull(version) ){
						version = null;
					}
				}
				Short order = resourcesConfigs.get(i).getDisplayOrder();
				Timestamp begin = resourcesConfigs.get(i).getActionDate();//开始时间
				Timestamp end = resourcesConfigs.get(i).getEndDate();//结束时间
				//隐藏Timestamp的毫秒，转化为String类型
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
				String beginStr = df.format(begin);  
				String endStr = df.format(end);
				pw.print(reid+"|");
				pw.print(type+"|");
				pw.print(resourcesName+"|");
				pw.print(resourcesURL+"|");
				pw.print(version+"|");
				pw.print(beginStr+"|");
				pw.print(endStr+"|");
				pw.print(order);
				pw.print("&");
				pw.println();
			}		
			pw.close();
			String listUrl = PropertyUtil.configProperty.getProperty("resource_http_base_path") + "/" + 
					PropertyUtil.configProperty.getProperty("resource_ConfigListTxt") 
					+ "/" + configListId + ".txt";
			configList.setListUrl(listUrl);
			configList.setIsGenerationTxt((short)1);
			configListService.saveConfigList(configList);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "redirect:/configList/toConfigListPage.html";
	}
}
